package com.gym.system.appointment.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.gym.common.core.domain.entity.SysRole;
import com.gym.common.core.domain.entity.SysUser;
import com.gym.common.utils.DateUtils;
import com.gym.system.mapper.SysRoleMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gym.system.appointment.mapper.GymAppointmentMapper;
import com.gym.system.appointment.domain.GymAppointment;
import com.gym.system.appointment.service.IGymAppointmentService;
import com.gym.common.core.text.Convert;

/**
 * 预约Service业务层处理
 *
 * @author administrator
 * @date 2021-04-11
 */
@Service
public class GymAppointmentServiceImpl implements IGymAppointmentService {
    @Autowired
    private GymAppointmentMapper gymAppointmentMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询预约
     *
     * @param id 预约ID
     * @return 预约
     */
    @Override
    public GymAppointment selectGymAppointmentById(Long id) {
        return gymAppointmentMapper.selectGymAppointmentById(id);
    }

    /**
     * 查询预约列表
     *
     * @param gymAppointment 预约
     * @return 预约
     */
    @Override
    public List<GymAppointment> selectGymAppointmentList(GymAppointment gymAppointment) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        //如果当前用户非管理员身份 只能查自己的
        List<SysRole> sysRoles = sysRoleMapper.selectRolesByUserId(sysUser.getUserId());
        for (SysRole sysRole : sysRoles) {
            if (sysRole.getRoleId().equals(1L)) {
                return gymAppointmentMapper.selectGymAppointmentList(gymAppointment);
            }
        }
        gymAppointment.setAppointUser(sysUser.getUserName());
        return gymAppointmentMapper.selectGymAppointmentList(gymAppointment);
    }

    /**
     * 新增预约
     *
     * @param gymAppointment 预约
     * @return 结果
     */
    @Override
    public int insertGymAppointment(GymAppointment gymAppointment) {
        return gymAppointmentMapper.insertGymAppointment(gymAppointment);
    }

    /**
     * 修改预约
     *
     * @param gymAppointment 预约
     * @return 结果
     */
    @Override
    public int updateGymAppointment(GymAppointment gymAppointment) {
        gymAppointment.setUpdateTime(DateUtils.getNowDate());
        return gymAppointmentMapper.updateGymAppointment(gymAppointment);
    }

    /**
     * 删除预约对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGymAppointmentByIds(String ids) {
        return gymAppointmentMapper.deleteGymAppointmentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预约信息
     *
     * @param id 预约ID
     * @return 结果
     */
    @Override
    public int deleteGymAppointmentById(Long id) {
        return gymAppointmentMapper.deleteGymAppointmentById(id);
    }
}
